package com.emagic.manage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;

import com.emagic.manage.common.preference.UserSharedPreference;
import com.emagic.manage.data.entities.response.UserResponse;
import com.emagic.manage.data.user.ContextHolder;
import com.emagic.manage.injections.components.DaggerGlobalComponent;
import com.emagic.manage.modules.loginmodule.activity.LoginActivity;
import com.emagic.manage.navigation.Navigator;
import com.xitaiinfo.library.instrumentation.ApplicationInstrumentation;
import com.xitaiinfo.library.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.support.DaggerApplication;

import static com.emagic.manage.data.user.ContextHolder.getContext;


public class LifeApplication extends DaggerApplication {

    private static LifeApplication instance;
    private List<Activity> activitiesList = new ArrayList<Activity>();
    private UserResponse currentUser;      //当前用户

    @Inject
    ApplicationInstrumentation mInstrumentation;

    @Inject
    Navigator mNavigator;


    public static LifeApplication getInstance() {
        return instance;
    }


    public Navigator getNavigator() {
        return mNavigator;
    }

    //support multiDex
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        ContextHolder.initial(this);  //data工程初始化Context对象引用
        mInstrumentation.init();
        CommonUtils.setupApplicationEnv(new CommonUtils.ApplicationEnv("chixiaOA"));
    }

    public void outOffSuccess(Context context) {
        UserSharedPreference preference = UserSharedPreference.getInstance(context);
        preference.clear();
        String phone = LifeApplication.getInstance().getCurrentUser().getAccount();
        LifeApplication.getInstance().release();
        Intent intentToLaunch = LoginActivity.getCallingIntent(getContext(), phone);
        intentToLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intentToLaunch);
    }


    public UserResponse getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(UserResponse currentUser) {
        this.currentUser = currentUser;
    }

    public String getUID() {
        return currentUser == null ? "" : currentUser.getUserid();
    }

    public void release() {
        this.currentUser = null;
    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerGlobalComponent.builder().create(this);

    }
    public void addActivity(Activity activity) {
        if (!activitiesList.contains(activity)) activitiesList.add(activity);
    }

    public void removeActivity(Activity activity) {
        if (activitiesList.contains(activity)) activitiesList.remove(activity);
    }
    public void systemExit() {
        for (Activity activity : activitiesList) activity.finish();
        System.gc();
    }
}
